package com.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dao.UploadImageDAO;
import com.model.UploadImage;

@Repository
public class UploadImageDAOImpl implements UploadImageDAO {
	@Autowired
	private SessionFactory sessionFactory;

	public int write(UploadImage image) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(image);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return image.getId();
	}

	public UploadImage read(String path) {
		Session s = sessionFactory.getCurrentSession();
		String hql = "select img From UploadImage img where img.path=:path";
		
		Query query = s.createQuery(hql);
		query.setString("path", path);

		List<UploadImage> list = (List<UploadImage>) query.list();
		if(list.size()>0){
			return (UploadImage) list.get(0);
		}else{
			return null;
		}
	}

	public UploadImage read(int id) {
		// TODO Auto-generated method stub
		UploadImage image = (UploadImage) sessionFactory.getCurrentSession().get(
				UploadImage.class, id);
		return image;
	}

}
